//
//  JXLStatusToolBarView.swift
//  MicroBlog
//
//  Created by 贾晓磊 on 16/9/24.
//  Copyright © 2016年 贾晓磊. All rights reserved.
//

import UIKit

class JXLStatusToolBarView: UIView {

    //添加按钮
    private lazy var retweetButton: UIButton = self.addChildButton(title: "转发", imageName: "timeline_icon_retweet")
    private lazy var commontButton: UIButton = self.addChildButton(title: "评论", imageName: "timeline_icon_comment")
    private lazy var unlikeButton: UIButton = self.addChildButton(title: "赞", imageName: "timeline_icon_unlike")
    
    var statusViewModel:JXLStatusViewModel? {
        didSet {
    
            retweetButton.setTitle(statusViewModel?.retweetCountContent, for: .normal)
            commontButton.setTitle(statusViewModel?.commentCountContent, for: .normal)
            unlikeButton.setTitle(statusViewModel?.unlikeCountContent, for: .normal)
        }
    }
    
    //重写构造方法
   override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //布局和添加子控件
   private func setupUI() {
    
    //布局子控件
    //布局转发按钮
    retweetButton.snp_makeConstraints { (make) in
        make.leading.top.bottom.equalTo(self)
        make.width.equalTo(commontButton)
    }
    
    //布局评论按钮
    commontButton.snp_makeConstraints { (make) in
        make.top.bottom.equalTo(self)
        make.leading.equalTo(retweetButton.snp_trailing)
        make.width.equalTo(unlikeButton)
    }
    
    //布局点赞按钮
    unlikeButton.snp_makeConstraints { (make) in
        make.top.trailing.bottom.equalTo(self)
        make.leading.equalTo(commontButton.snp_trailing)
    }
    
    //添加竖线view
    let firstLineView = addChildLineView()
    let secondLineView = addChildLineView()

    //布局第一个竖线view
    firstLineView.snp_makeConstraints { (make) in
        make.centerX.equalTo(retweetButton.snp_trailing)
        make.centerY.equalTo(self)
    }
     //布局第二个竖线view
    secondLineView.snp_makeConstraints { (make) in
        make.centerX.equalTo(commontButton.snp_trailing)
        make.centerY.equalTo(self)
    }

    
    }
    
    //添加button
    private func addChildButton(title: String, imageName:String) -> (UIButton) {
        let button = UIButton()
        //设置按钮标题
        button.setTitle(title, for: .normal)
        //设置按钮图片
        button.setImage(UIImage(named:imageName), for: .normal)
        //设置字体大小
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        //设置字体颜色
        button.setTitleColor(UIColor.darkGray, for: .normal)
        //设置背景图片
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), for: .normal)
        button.adjustsImageWhenHighlighted = false
        //添加到父控件
        addSubview(button)
        return button
        
    }
    
    //创建竖线视图
    private func addChildLineView() -> UIImageView {
        let imageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
        addSubview(imageView)
        return imageView

        
    }
}
